Golang encoding/xml.Encoder.Flush() function example
package encoding/xml
Flush flushes any buffered XML to the underlying writer. See the EncodeToken documentation for details about when it is necessary.
Golang encoding/xml.Encoder.Flush() function usage example
package main
import (
"encoding/xml"
"fmt"
"os"
"reflect"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
encoder := xml.NewEncoder(os.Stdout)
newtoken := xml.StartElement{
Name : xml.Name{
Space: "",
Local: "location",
},
}
if err := encoder.EncodeToken(newtoken); err != nil {
fmt.Println(err)
}
start := xml.StartElement{
Name: xml.Name{
Space: "",
Local: reflect.Indirect(reflect.ValueOf(v)).Type().Name(),
},
Attr: []xml.Attr{{xml.Name{"", "xmlns"}, "https://socketloop.com/xmldoc/2014-09-01/"}},
}
if err := encoder.EncodeElement(v, start); err != nil {
fmt.Println(err)
}
encoder.Flush() // <-- here
}
Reference :
Advertisement
Something interesting
Tutorials
+7.1k Nginx : How to block user agent ?
+9.6k Golang : How to generate Code 39 barcode?
+18.9k Golang : Read input from console line
+8.8k Golang : HTTP Routing with Goji example
+11.6k Get form post value in Go
+20.7k Golang : Read directory content with os.Open
+62.7k Golang : Convert HTTP Response body to string
+30k Golang : Get time.Duration in year, month, week or day
+7.5k Golang : Shuffle strings array
+4.7k Fix Google Analytics Redundant Hostnames problem
+14.4k Golang : How to pass map to html template and access the map's elements
+9.6k Golang : Read file with ioutil